package com.xiaozhengkeji.springcloud.ezcloud.api;


import com.xiaozhengkeji.springcloud.ezcloud.common.core.base.entitys.user.SystemUserEntity;
import com.xiaozhengkeji.springcloud.ezcloud.common.core.base.services.user.SystemUserEntityService;
import com.xiaozhengkeji.springcloud.ezcloud.common.core.bean.RetResult;
import com.xiaozhengkeji.springcloud.ezcloud.common.core.model.CodeEnum;
import com.xiaozhengkeji.springcloud.ezcloud.servicebus.UserServiceBus;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/system_user")
public class SystemUserApi {
    SystemUserEntityService systemUserEntityService;
    UserServiceBus userServiceBus;

    @Autowired
    public SystemUserApi(SystemUserEntityService systemUserEntityService, UserServiceBus userServiceBus) {
        this.systemUserEntityService = systemUserEntityService;
        this.userServiceBus = userServiceBus;
    }

    @ApiOperation("获取指定uid的用户info")
    @GetMapping("/infoByUid")
    public SystemUserEntity infoByUid(@ApiParam("用户UID") @RequestParam("uid") Integer uid) {
        return systemUserEntityService.selectByPrimaryKey(uid);
    }

    @ApiOperation("获取指定用户名的用户info ps:返回列表")
    @GetMapping("/infoByUsername")
    public List<SystemUserEntity> infoByUsername(@ApiParam("用户名") @RequestParam("username") String username,
                                                 @ApiParam("是否模糊查找") @RequestParam("isLike") Boolean isLike) {
        if (isLike) {
            return systemUserEntityService.findByUsernameLike(username);
        }
        return systemUserEntityService.queryByUsername(username);
    }

    @ApiOperation("获取指定邮箱的用户info ps:返回列表")
    @GetMapping("/infoByMailbox")
    public List<SystemUserEntity> infoByMailbox(@ApiParam("用户邮箱") @RequestParam("mailbox") String mailbox,
                                   @ApiParam("是否模糊查找") @RequestParam("isLike") Boolean isLike) {
        if (isLike) {
            return systemUserEntityService.findByMailboxLike(mailbox);
        }
        return systemUserEntityService.queryByMailbox(mailbox);
    }

    @ApiOperation("注册一个用户")
    @PostMapping("/register")
    public Integer register(@ApiParam("用户名") @RequestParam("username") String username,
                              @ApiParam("密码") @RequestParam("password") String password) {
        return userServiceBus.registeredUser(username, password);
    }


}
